using System;
using System.Collections.Generic;
using System.Linq;

namespace ClientJIP
{
	public class CtrlChat
	{
		private MainWindow View;
		private Chat ChatModel;
		
		public CtrlChat (MainWindow view)
		{
			View = view;
			ChatModel = new Chat(this);
		}
		
		public void WriteTextLog(string message)
		{
			View.WriteTextLog(message);
		}
		
		public void WriteTextPublic(string message, string pseudoWriter)
		{
			View.WriteTextPublic(message, pseudoWriter);
		}

		public void WriteTextPrivate(string message, string pseudo_write, string pseudoPage)
		{
			View.WriteTextPrivate(message, pseudo_write, pseudoPage);
		}
		
		public void writeToServeur(string message) {
			ChatModel.WriteLine(message);
		}
		
		public void ConnectedToServeur(string host)
		{
			WriteTextLog("Serveur selectionné : " + host);
		
			if(ChatModel.ConnectToServeur(host)) {
				WriteTextLog("Connexion établie");
				WriteTextLog("Veuillez saisir votre nom");
				
				View.SetInterfaceConnected(true);
			}
	
			else
				WriteTextLog("Connexion au serveur echouée");
		}
		
		public void ConnectedToChat()
		{
			View.ConnectToChat();
			if(ChatModel.Pseudo != "")
	        	WriteTextPublic("Connecté avec le pseudo " + ChatModel.Pseudo + ".", "");
		}
		
		
		public bool SendToChat(string message, int page)
		{
			return ChatModel.SendCommand(message, page);
		}
		

		public void QuitFromChat() 
		{
			View.ResetPublic();
			View.QuitChatSession();
		}
		
		public void DeletePageTalk(int index)
		{
			View.DeletePageTalk(index);
		}
		
		public void quitChat()
		{
			ChatModel.DisconnectServeur();
		}
		
		
		public void ChangeNick(String oldPseudo, String newPseudo)
		{
	        View.WriteTextPublic(oldPseudo + " est maintenant connu comme " + newPseudo + ".", "");
			View.ChangeNick(oldPseudo, newPseudo);
		}
		
		public void SwitchToAfk()
		{
			View.SwitchToAfk();
		}
		
		
		public void SwitchToNfk()
		{
			View.SwitchToNfk();
			
			ChatModel.WriteLine("/LIST_NFK");
		}
	
		public void WriteListUser(Dictionary<string, int> connected){
			View.WriteListUser(connected);
		}
		public void WriteListUser() {
			WriteListUser(ChatModel.Connected);
		}
		
		public void SendAskTalk(string pseudo)
		{			
			WriteTextPublic("Demande de conversation avec " + pseudo + ".", "");
			ChatModel.WriteLine("/TALK " + pseudo);
		}
		
		public void AskChooseConversation(string pseudo)
		{
			View.WindowsAskTalk(pseudo);
		}
		
		public void AcceptTalk(string pseudoTalker) {
			View.AcceptTalk(pseudoTalker);
	        WriteTextPublic(pseudoTalker + " accepte d'avoir une conversation privée.", "");
		}

		public String GetNick()
		{
			return ChatModel.Pseudo;
		}
		
		public void FilesToSendClient(String fileName, String pathFileName, String tmpPseudo,long fileSize)
		{
			ChatModel.FilesToSendClient(fileName,pathFileName, tmpPseudo, fileSize);	
		}
		
		public void AskChooseFileTransfert(String pseudo)
		{
			View.WindowsAskFile(pseudo);
		}
		
		public void AcceptFile(String pseudo)
		{
			ChatModel.AcceptFile(pseudo);
		}
		
		public void RefuseFile(String pseudo)
		{
			ChatModel.RefuseFile(pseudo);
		}
		
		public int GetNbPage() {
			return View.GetNbPage();
		}
		
		public string ConnectedAtPage(int index)
		{
			var tmp = (from entry in ChatModel.Connected where entry.Value != -1 select entry.Key);
			
			return tmp.FirstOrDefault();
		}
		
		
		public void AddConnectionPage(int page, string pseudo)
		{
			ChatModel.Connected[pseudo] = page;
		}
		
		public void CloseConv(int page) 
		{
			ChatModel.WriteLine("/CLOSE_TALK " + ConnectedAtPage(page));
		}
	}
}

